package org.galaxyworld.flycode.gui;

import org.galaxyworld.flycode.events.MainWindowEvent;
import org.galaxyworld.flycode.events.MainWindowEventListener;

import javax.swing.*;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.LinkedList;
import java.util.List;

public class MainWindow extends JFrame {

    public static final int GAP = 8;

    private final List<MainWindowEventListener> mainWindowEventListenerList = new LinkedList<>();

//        JButton startButton = new JButton("Start");
//        startButton.addActionListener(new ActionListener() {
//            @Override
//            public void actionPerformed(ActionEvent e) {
//                ModelGenerationTask task = new ModelGenerationTask(outputArea);
//                task.execute();
//            }
//        });

    public MainWindow() {
        setTitle("FlyCode");
        setSize(800, 600);
        setLocationRelativeTo(null);

        addWindowListener(new WindowAdapter() {

            @Override
            public void windowOpened(WindowEvent e) {
                for (MainWindowEventListener listener : mainWindowEventListenerList) {
                    listener.windowOpened(new MainWindowEvent(MainWindowEvent.Id.Opened, this));
                }
            }

            @Override
            public void windowClosing(WindowEvent e) {
                for (MainWindowEventListener listener : mainWindowEventListenerList) {
                    listener.windowClosing(new MainWindowEvent(MainWindowEvent.Id.Closing, this));
                }
//                int result = JOptionPane.showConfirmDialog(null, "Do you want to save the configuration?", "Exit", JOptionPane.YES_NO_OPTION);
//                if (result == JOptionPane.YES_OPTION) {
//                    ConfigFile.setValue("jdbc.driver", jdbcDriverField.getText());
//                    ConfigFile.setValue("jdbc.url", jdbcUrlField.getText());
//                    ConfigFile.setValue("jdbc.username", jdbcUserNameField.getText());
//                    ConfigFile.setValue("jdbc.password", jdbcPasswordField.getText());
//                    ConfigFile.setValue("jdbc.prefix", jdbcPrefixField.getText());
//                    ConfigFile.setValue("java.package", javaPackageField.getText());
//                    ConfigFile.setValue("java.includePrefix", destIncludePrefixBox.isSelected() ? "true" : "false");
//                    ConfigFile.setValue("output.dir", destOutputDirField.getText());
//                    ConfigFile.save();
//                }
                System.exit(0);
            }
        });

        addPropertyChangeListener("last", (e) -> {
            System.out.println("fire!");
        });

        Container container = getContentPane();
        container.setLayout(new BorderLayout());

        final JPanel mainPane = new JPanel();
        final CardLayout mainLayout = new CardLayout(GAP, GAP);
        mainPane.setLayout(mainLayout);
        container.add(mainPane, BorderLayout.CENTER);

        JdbcConfigPane jdbcConfigPane = new JdbcConfigPane();
        mainPane.add(jdbcConfigPane);
        ModelConfigPane modelConfigPane = new ModelConfigPane();
        mainPane.add(modelConfigPane);
        ServiceConfigPane serviceConfigPane = new ServiceConfigPane();
        mainPane.add(serviceConfigPane);
        AppConfigPane appConfigPane = new AppConfigPane();
        mainPane.add(appConfigPane);
        ConclusionPane conclusionPane = new ConclusionPane();
        mainPane.add(conclusionPane);

        JPanel southPane = new JPanel();
        southPane.setLayout(new FlowLayout());
        container.add(southPane, BorderLayout.SOUTH);

        JButton previous = new JButton("Previous");
        previous.addActionListener((e) -> {
            mainLayout.previous(mainPane);
        });
        southPane.add(previous);
        JButton next = new JButton("Next");
        next.addActionListener((e) -> {
            mainLayout.next(mainPane);
        });
        southPane.add(next);

        mainWindowEventListenerList.add(jdbcConfigPane);
        mainWindowEventListenerList.add(modelConfigPane);
        mainWindowEventListenerList.add(serviceConfigPane);
        mainWindowEventListenerList.add(appConfigPane);
    }

}
